package com.zys.http.ui.tab;

import com.intellij.openapi.project.Project;
import com.zys.http.constant.HttpEnum;
import com.zys.http.entity.ReqHistory;
import com.zys.http.entity.param.ParamProperty;
import com.zys.http.extension.service.Bundle;
import com.zys.http.tool.Placeholder;
import com.zys.http.ui.tab.panel.*;
import jdk.jfr.Description;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhou ys
 * @since 2023-10-20
 */
@Getter
@Description("API 页面的标签选项栏")
public class RequestTabs extends JTabbedPane {

    @Description("是否需要选择响应体的类型")
    private final boolean resTabNeedFileType;

    private final RequestHeaderPanel requestHeaderPanel;
    private final RequestParamPanel requestParamPanel;
    private final RequestBodyPanel requestBodyPanel;
    private final RequestFilePanel requestFilePanel;
    private final ResponsePanel responsePanel;

    public RequestTabs(@NotNull Project project) {
        this(project, false);
    }

    public RequestTabs(@NotNull Project project, boolean resTabNeedFileType) {
        this.resTabNeedFileType = resTabNeedFileType;

        this.requestHeaderPanel = new RequestHeaderPanel(project);
        this.addTab(Bundle.get("http.api.tab.header"), requestHeaderPanel);

        this.requestParamPanel = new RequestParamPanel(project);
        this.addTab(Bundle.get("http.api.tab.param"), requestParamPanel);

        this.requestBodyPanel = new RequestBodyPanel(project);
        this.addTab(Bundle.get("http.api.tab.body"), requestBodyPanel);

        this.requestFilePanel = new RequestFilePanel(project);
        this.addTab(Bundle.get("http.api.tab.file"), requestFilePanel);

        this.responsePanel = new ResponsePanel(project, resTabNeedFileType);
        this.addTab(Bundle.get("http.api.tab.response"), responsePanel);
    }

    public void reset() {
        this.requestHeaderPanel.reset();
        this.requestParamPanel.reset();
        this.requestFilePanel.reset();
        this.requestBodyPanel.reset();
        this.responsePanel.reset();
    }

    public void chooseEvent(HttpEnum.HttpMethod httpMethod, HttpEnum.ContentType contentType, @NotNull Map<String, ParamProperty> paramPropertyMap) {
        for (Map.Entry<String, ParamProperty> entry : paramPropertyMap.entrySet()) {
            ParamProperty v = entry.getValue();
            HttpEnum.ParamUsage usage = v.getParamUsage();
            this.requestHeaderPanel.execute(httpMethod, contentType, entry.getKey(), paramPropertyMap);
            this.requestParamPanel.execute(httpMethod, contentType, entry.getKey(), paramPropertyMap);
            this.requestBodyPanel.execute(httpMethod, contentType, entry.getKey(), paramPropertyMap);
            this.requestFilePanel.execute(httpMethod, contentType, entry.getKey(), paramPropertyMap);
            switch (usage) {
                case PATH -> this.setSelectedComponent(requestParamPanel);
                case URL ->
                        this.setSelectedComponent(httpMethod.equals(HttpEnum.HttpMethod.POST) ? requestBodyPanel : requestParamPanel);
                case BODY -> this.setSelectedComponent(requestBodyPanel);
                case FILE -> this.setSelectedComponent(requestFilePanel);
                default -> {
                    // 不处理
                }
            }
        }
    }

    @Description("结果状态文本")
    public void resultText(int status) {
        this.responsePanel.resultText(status);
    }

    public void recoverHistory(ReqHistory history) {
        this.requestHeaderPanel.recover(history);
        this.requestParamPanel.recover(history);
        this.requestFilePanel.recover(history);
        this.requestBodyPanel.recover(history);
        this.responsePanel.recover(history);
    }

    public Map<String, String> headers() {
        return this.requestHeaderPanel.getHeaderTable().buildHttpHeader();
    }

    public Map<String, String> parameters() {
        return this.requestParamPanel.getParameterTable().buildHttpHeader();
    }

    public String body() {
        String body = this.requestBodyPanel.getBodyEditor().getText();

        List<String> matches = new ArrayList<>();
        // 正则说明：匹配 {{$ 开头 + 单词字符 + }} 结尾
        String regex = "\\{\\{\\$(.*?)}}";
        Matcher matcher = Pattern.compile(regex).matcher(body);

        while (matcher.find()) {
            matches.add(matcher.group());
        }
        for (String match : matches) {
            body = body.replace(match, String.valueOf(Placeholder.replacePlaceholder(match)));
        }
        // 处理占位符
        return body;
    }

    public String[] files() {
        return this.requestFilePanel.getFileUploadTable().fileNames();
    }

    public String filePartName() {
        return this.requestFilePanel.getPartTextField().getText();
    }
}
